import Vue from 'vue'
import filter from '@/filter'
// 绑定axios
import service from './request'
// 绑定localStorage 和 sessionStorage
import storage from './storage'
// 路由守卫配置
import './routerGuard'

Vue.prototype.$axios = service;

Vue.prototype.$storage = storage;

import {
  Breadcrumb, Button, Carousel, Dropdown, Icon, Input, Menu, message, Anchor, Layout, Steps, Divider, FormModel, Checkbox, Row, Col, Upload,
  Radio, Select, Modal, Tooltip, Collapse, Tag, Cascader, DatePicker,TreeSelect,Table,BackTop, Drawer,Timeline,InputNumber,Popconfirm, AutoComplete,
  Tree
} from 'ant-design-vue'
Vue.use(Button).use(Breadcrumb).use(Carousel).use(Icon).use(Input).use(Dropdown).use(Menu).use(Anchor).use(Layout).use(Divider).use(Steps)
  .use(FormModel).use(Checkbox).use(Row).use(Col).use(Upload).use(Radio).use(Select).use(Modal).use(Tooltip).use(Collapse).use(Tag).use(Cascader)
  .use(DatePicker).use(TreeSelect).use(Table).use(BackTop).use(Drawer).use(Timeline).use(InputNumber).use(Popconfirm).use(AutoComplete).use(Tree);

Vue.prototype.$message = message;

import Vuebar from 'vuebar';
Vue.use(Vuebar);

// 注册过滤器
for (let key in filter) {
  if (filter[key])
    Vue.filter(key, filter[key]);
}

// 注册全局方法
import jsFunc from './jsFunc';
for (let key in jsFunc) {
  Vue.prototype['$' + key] = jsFunc[key];
}

/**
 * 时间对象的格式化;
 */
Date.prototype.format = function (format, now) {
  /*
   * eg:format="yyyy-MM-dd hh:mm:ss";
   */
  var d = now ? (new Date(Date.parse(now.replace(/-/g, "/")))) : this;
  var o = {
    "M+": d.getMonth() + 1, // month
    "d+": d.getDate(), // day
    "h+": d.getHours(), // hour
    "m+": d.getMinutes(), // minute
    "s+": d.getSeconds(), // second
    "q+": Math.floor((d.getMonth() + 3) / 3), // quarter
    "N+": ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"][d.getDay()],
    "S": d.getMilliseconds()// millisecond
  }

  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4
      - RegExp.$1.length));
  }

  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? o[k]
        : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
};

const apiFiles = require.context('./../api', true, /\.js$/);

// you do not need `import api from './../api'`
// it will auto require all api module from modules file
const api = apiFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const moduleNameList = moduleName.split('/');
  let value = apiFiles(modulePath);
  if(moduleNameList.length > 1) {
    if(moduleNameList.indexOf('index') !== -1) {
      modules[moduleNameList[0]] = value.default
    }
  } else {
    modules[moduleName] = value.default
  }
  return modules
}, {});

Vue.prototype.$api = api;
